package com.tinyengine.it.controller.ai;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tinyengine.it.common.base.Result;
import com.tinyengine.it.common.enums.AiType;
import com.tinyengine.it.common.utils.DeepseekUtils;
import com.tinyengine.it.common.utils.TranslateUtils;
import com.tinyengine.it.model.dto.TransResultDTO;
import com.tinyengine.it.model.entity.SysTermCache;
import com.tinyengine.it.service.db.SysTermCacheService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/auto/features")
@Tag(name = "自助功能模块")
@RequiredArgsConstructor
@Slf4j
public class AutoFeaturesController {

    private final SysTermCacheService sysTermCacheService;

    @Operation(summary = "一键生成",
            parameters = {
                    @Parameter(name = "keywords", description = "需要生成的关键字，格式：姓名，性别，体重，身高")
            }
    )
    @PostMapping("/generation")
    public Result<JSONArray> generation(@RequestBody Params params) {

        JSONArray jsonArray;
        try {
            jsonArray = DeepseekUtils.DeepseekAi(params.getKeywords(), params.getAiType());

            try {
                for (Object o : jsonArray) {
                    JSONObject jsonObject = (JSONObject) o;
                    Object description = jsonObject.get("description");
                    SysTermCache termCache = sysTermCacheService.getOne(new LambdaQueryWrapper<SysTermCache>().eq(SysTermCache::getName, description).last("limit 1"));
                    if (ObjUtil.isNotNull(termCache)) {
                        ((JSONObject) o).put("key", termCache.getEnName());
                    }
                }
            } catch (Exception e) {
                log.error(e.getMessage());
            }

        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
        return Result.success(jsonArray);
    }

    @Operation(summary = "翻译",
            parameters = {
                    @Parameter(name = "keywords", description = "需要翻译的关键字数组")
            }
    )
    @PostMapping("/translation")
    public Result<List<TransResultDTO>> translation(@RequestBody ParamsList params) {
        List<TransResultDTO> transResultDTOS = new ArrayList<>();
        try {
            List<String> keywords = params.getKeywords();
            for (String keyword : keywords) {
                if (ObjUtil.isNull(keyword)) {
                    continue;
                }
                TransResultDTO translate;
                List<SysTermCache> sysTermCaches = sysTermCacheService.list(new LambdaQueryWrapper<SysTermCache>().eq(SysTermCache::getName, keyword));
                if (!sysTermCaches.isEmpty()) {
                    SysTermCache sysTermCache = sysTermCaches.get(0);
                    String enName = sysTermCache.getEnName();
                    translate = new TransResultDTO();
                    translate.setSrc(keyword);
                    translate.setDst(enName);
                } else {
                    translate = TranslateUtils.translate(keyword);
                    translate.setDst(translate.getDst().replaceAll(" ", "_"));
                    //translate.setDst(StrUtil.toUnderlineCase(translate.getDst()));
                }
                switch (params.getAiType()) {
                    case 下划线:
                        translate.setDst(StrUtil.toUnderlineCase(translate.getDst()).toLowerCase());
                        break;
                    default:
                        translate.setDst(StrUtil.lowerFirst(StrUtil.toCamelCase(translate.getDst())));
                        break;
                }
                transResultDTOS.add(translate);
            }
        } catch (Exception e) {
            throw new RuntimeException("翻译失败，请重试！！！！");
        }
        return Result.success(transResultDTOS);
    }
}

@Data
class ParamsList {
    private List<String> keywords;
    private AiType aiType;
}

@Data
class Params {
    private String keywords;
    private AiType aiType;
}
